package com.epam.newsmanagement.presentation.action;


import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.epam.newsmanagement.database.dao.INewsDAO;
import com.epam.newsmanagement.model.News;
import com.epam.newsmanagement.presentation.form.NewsForm;
import com.epam.newsmanagement.util.DateConverter;

public final class NewsAction extends DispatchAction {
	private static final String LIST_METHOD_PATH = "listMethod";
	private static final String VIEW_METHOD_PATH = "viewMethod";
	private static final String NEWS_LIST_PATH = "newsListContent";
	private static final String VIEW_NEWS_PATH = "viewNewsContent";
	private static final String ADD_NEWS_PATH = "addNewsContent";
	private static final String SUFFIX = "Content";
	private static final String CONTENT_TYPE = "text/xml";
	private static final String CACHE_CONTROL_HEADER = "Cache-Control";
	private static final String CACHE_CONTROL_VALUE = "no-cache";
	
	private INewsDAO newsDAO;
	
	public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			NewsForm newsForm = (NewsForm)form;
			List<News> newsList = newsDAO.getList();
			newsForm.setNewsList(newsList);
			newsForm.setCurrentNewsId(0);
			newsForm.setSelectedNews(null);
			newsForm.setCancelPath(LIST_METHOD_PATH);
			newsForm.setCurrentPagePath(NEWS_LIST_PATH);
			return mapping.findForward("newsList");
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
								HttpServletResponse response) throws Exception {
		NewsForm newsForm = (NewsForm)form;
		List<News> newsList = newsDAO.getList();
		newsForm.setNewsList(newsList);
		newsForm.setCurrentNewsId(0);
		newsForm.setSelectedNews(null);
		newsForm.setCancelPath(LIST_METHOD_PATH);
		newsForm.setCurrentPagePath(NEWS_LIST_PATH);
		prepareResponse(response);
		return mapping.findForward(NEWS_LIST_PATH);
	}
	public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request,
								HttpServletResponse response) throws Exception {
		NewsForm newsForm = (NewsForm)form;
		int id = newsForm.getCurrentNewsId();
		List<News> newsList = newsForm.getNewsList();
		for(News news:newsList){
			if(id == news.getId()){
				newsForm.setNews(news);
			}
		}
		newsForm.setCurrentPagePath(VIEW_NEWS_PATH);
		newsForm.setCancelPath(VIEW_METHOD_PATH);
		prepareResponse(response);
		return mapping.findForward(VIEW_NEWS_PATH);
	}
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
								HttpServletResponse response) throws Exception {
		NewsForm newsForm = (NewsForm)form;
		int id = newsForm.getCurrentNewsId();
		List<News> newsList = newsForm.getNewsList();
		for(News news:newsList){
			if(id == news.getId()){
				newsForm.setNews(news);
			}
		}
		newsForm.setCurrentPagePath(ADD_NEWS_PATH);
		prepareResponse(response);
		return mapping.findForward(ADD_NEWS_PATH);
	}
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
								HttpServletResponse response) throws Exception {
		
		NewsForm newsForm = (NewsForm)form;
		Integer[] selectedNews = newsForm.getSelectedNews();
		if(selectedNews != null){
			newsDAO.remove(selectedNews);
		} else {
			int currentNewsId = newsForm.getCurrentNewsId();
			newsDAO.remove(new Integer[]{currentNewsId});
		}
		return mapping.findForward(LIST_METHOD_PATH);
	}
	public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
								HttpServletResponse response) throws Exception {
		NewsForm newsForm = (NewsForm)form;
		String cancelPath = newsForm.getCancelPath();
		return mapping.findForward(cancelPath);
	}
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
								HttpServletResponse response) throws Exception {
		NewsForm newsForm = (NewsForm)form;
		News news = newsForm.getNews();
		HttpSession session = request.getSession();
		Integer inputId = news.getId();
		Locale locale = (Locale)session.getAttribute(Globals.LOCALE_KEY);
		String dateString =  newsForm.getDateString();
		Date date = DateConverter.convert(dateString, locale);
		news.setDate(date);
		newsDAO.save(news);
		if(inputId == null){
			newsForm.getNewsList().add(news);
			newsForm.setCurrentNewsId(news.getId());
		}
		return mapping.findForward(VIEW_METHOD_PATH);
	}
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
								HttpServletResponse response) throws Exception {
		NewsForm newsForm = (NewsForm)form;
		News defaultNews = new News();
		defaultNews.setDate(new Date());
		newsForm.setNews(defaultNews);
		newsForm.setCurrentPagePath(ADD_NEWS_PATH);
		prepareResponse(response);
		return mapping.findForward(ADD_NEWS_PATH);
	}
	public ActionForward language(ActionMapping mapping, ActionForm form, HttpServletRequest request,
									HttpServletResponse response) throws Exception {
		NewsForm newsForm = (NewsForm)form;
		HttpSession session = request.getSession();
		Locale locale = new Locale(newsForm.getLanguage());
		session.setAttribute(Globals.LOCALE_KEY, locale);
		String currentPagePath = newsForm.getCurrentPagePath();
		int suffixStartIndex = currentPagePath.lastIndexOf(SUFFIX);
		String forwardPath = currentPagePath.substring(0, suffixStartIndex);
		return mapping.findForward(forwardPath);
	}
	public void setNewsDAO(INewsDAO newsDAO) {
		this.newsDAO = newsDAO;
	}
	
	private void prepareResponse(HttpServletResponse response){
		response.setContentType(CONTENT_TYPE);
		response.setHeader(CACHE_CONTROL_HEADER, CACHE_CONTROL_VALUE);
	}
}